﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyBanHang_N3A
{
    public partial class FormDatHang : Form
    {
       

        public FormDatHang()
        {
            InitializeComponent();
        }

        public string MaNV;

        void LayThongTinSanPham()
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham();
            colMaSP.DataSource = ds;
            colMaSP.DisplayMember = "MaSP";
            colMaSP.ValueMember = "MaSP";

        }
        int LayDonGia(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].DonGia;
        }
        string LayTenSanPham(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].TenSP;
        }
        string LayTenNhanVien(string maNV)
        {
            List<NhanVienDTO> ds = new List<NhanVienDTO>();
            NhanVienBUS bus = new NhanVienBUS();
            ds = bus.LayThongTinNhanVien(maNV);
            return ds[0].TenNV;
        }
   
        string KhoiTaoMaCTDH()
        {
            CTPhieuDatBUS bus = new CTPhieuDatBUS();
            return bus.TaoCTDHMoi();
        }
        string KhoiTaoMaDH()
        {
            PhieuDatBUS bus = new PhieuDatBUS();
            return bus.TaoMaDHMoi();
        
        }
        private void FormDatHang_Load(object sender, EventArgs e)
        {
   
            DateTime dh = DateTime.Now;
            lbNgayDat.Text = dh.ToString("dd/MM/yyyy");
            lbTenNV.Text = LayTenNhanVien(MaNV);
            lbMaDH.Text = KhoiTaoMaDH().ToString();            
            LayThongTinSanPham();
            lbTongTien.Text = "0";
            this.GridViewSanPham.Rows.Clear();
        }

        private void GridViewSanPham_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            if (GridViewSanPham.Rows[e.RowIndex].Cells[1].Value == null)
                return;
            string tenSP = LayTenSanPham(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString());
            if (tenSP == "")
                return;
            double dongia = Convert.ToDouble(LayDonGia(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString()));
            //int cvSP = Convert.ToInt32(LayCVSP(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString()));
            this.GridViewSanPham.Rows[e.RowIndex].Cells[2].Value = tenSP;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[4].Value = dongia;
            string soluong = GridViewSanPham.Rows[e.RowIndex].Cells[3].Value.ToString();
            double thanhtien = Convert.ToDouble(soluong) * dongia;
            //int diem = Convert.ToInt32(soluong) * cvSP;
            //this.GridViewSanPham.Rows[e.RowIndex].Cells[6].Value = diem;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[5].Value = thanhtien;
            double tongtien = 0;
           // int tongcv = 0;
            for (int i = 0; i < this.GridViewSanPham.Rows.Count; i++)
            {
                tongtien += Convert.ToDouble(this.GridViewSanPham.Rows[i].Cells[5].Value);
                //tongcv += Convert.ToInt32(this.GridViewSanPham.Rows[i].Cells[6].Value);

            }
            //lbTongCV.Text = tongcv.ToString();
            lbTongTien.Text = tongtien.ToString();
        }

        private void GridViewSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
        }

        private void GridViewSanPham_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            GridViewSanPham.Rows[e.RowIndex].Cells[3].Value = 0;
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.GridViewSanPham.Rows.Clear();
            this.lbTongTien.Text = "0";
            //this.lbTongCV.Text = "0";
            DateTime dh = DateTime.Now;
            lbNgayDat.Text = dh.ToString("dd//MM/yyyy");

        }

        private void btDatHang_Click(object sender, EventArgs e)
        {
            PhieuDatDTO pdDTO = new PhieuDatDTO();
            PhieuDatBUS pdbus = new PhieuDatBUS();
            pdDTO.MaPD = lbMaDH.Text.Trim();
            DateTime ngaydat = DateTime.ParseExact(this.lbNgayDat.Text, "dd/MM/yyyy", null);
            pdDTO.NgayDH = ngaydat;
            pdDTO.MaNV = MaNV.Trim();
            pdDTO.TongTien = Convert.ToDouble(lbTongTien.Text.ToString());
            int res = pdbus.ThemPhieuDat(pdDTO);
            if (res == 1)
            {
                res = 0;
                for (int i = 0; i < this.GridViewSanPham.Rows.Count - 1; i++)
                {
                    CTPhieuDatDTO ctpdDTO = new CTPhieuDatDTO();
                    ctpdDTO.MaCTPD =KhoiTaoMaCTDH().ToString();
                    ctpdDTO.MaPD = lbMaDH.Text.Trim();
                    ctpdDTO.MaSP = GridViewSanPham.Rows[i].Cells[1].Value.ToString();
                    ctpdDTO.SLDat = Convert.ToInt32(GridViewSanPham.Rows[i].Cells[3].Value);
                    ctpdDTO.DonGia = Convert.ToDouble(GridViewSanPham.Rows[i].Cells[4].Value);
                    ctpdDTO.ThanhTien = Convert.ToDouble(GridViewSanPham.Rows[i].Cells[5].Value);
                    CTPhieuDatBUS ctpdbus = new CTPhieuDatBUS();
                    res = ctpdbus.ThemCTPhieuDat(ctpdDTO);
         
                }
            }
            else
            {
                MessageBox.Show("Không Thành Công");
            }
            this.FormDatHang_Load(sender,e);
        }


        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kim tra
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column3")
            {
                //Là các ký tự số hoặc điều khiển
                if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column3")
            {
                try
                {

                }
                catch
                {
                    MessageBox.Show("Bạn phải nhập vào dữ liệu kiểu số.");
                }
            }
        }

        private void GridViewSanPham_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.GridViewSanPham.CurrentCellAddress.X == GridViewSanPham.Columns[3].DisplayIndex)
            {
                TextBox txt = e.Control as TextBox;
                if (GridViewSanPham.RowCount < 0)
                    return;
                txt.Name = "Column3";
                if (txt != null)
                {
                    txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                    txt.Validating -= new CancelEventHandler(txt_Validating);
                    txt.Validating += new CancelEventHandler(txt_Validating);
                }
            }
        }

      
    }
}
